//创建路由对象
//引用路由器对象
const express=require('express');
//创建路由器对象
const r=express.Router();
//引入pool文件
const pool=require('../pool.js');

//企业部分
//1.企业发布的职位详情接口
r.post('/zhiwei',(req,res,next)=>{
    var obj=req.body;
    console.log(obj);

    var i=400;
    for(var k in obj){
        i++;
        if(obj[k]==''){
            res.send({code:i,msg:k+'不能为空'});
            return;
        }
    }  
    pool.query('insert into com_zhiwei set?',[obj],(err,result)=>{
        if(err){
            next(err);
            return;
        }
        console.log(result);
        res.send({code:200,msg:'企业发布的职位详情表'})
    });
   
});


//2.删除企业发布的信息接口
r.delete('/delete/:czw_id',(req,res,next)=>{ 

    pool.query('delete  from com_zhiwei where czw_id=?',[req.params.czw_id],(err,result)=>{
        console.log(result);
        console.log(req.params.uid)
        if(err){
        next(err);
        return;
    }
    console.log(result)
         if(result.affectedRows===0){
	  res.send({code:501,msg:'该信息不存在，删除失败'});
	}else{
	  res.send({code:200,msg:'删除成功',data:result});
	}
        
    });
});


//3.接受到的简历记录接口
r.post('/comdelivery',(req,res,next)=>{
    var obj=req.body;
    console.log(obj);

    var i=400;
    for(var k in obj){
        i++;
        if(obj[k]==''){
            res.send({code:i,msg:k+'不能为空'});
            return;
        }
    }  
    pool.query('insert into com_delivery set?',[obj],(err,result)=>{
        if(err){
            next(err);
            return;
        }
        console.log(result);
        res.send({code:200,msg:'接受到的简历记录表'})
    });
   
});


//4.所有企业发布的信息列表(有错)
r.get('/com/:ye/:tiao',(req,res,next)=>{
    console.log(req.params);//{ ye: '0', tiao: '1' }
    console.log(Number(req.params.ye.toString()));//1
    
    var obj=req.params;
     pool.query('select * from com_zhiwei limit ?,?',[Number(obj.ye.toString()),Number(obj.tiao.toString())],(err,result)=>{
        console.log(result);
        if(err){
            next(err);
            return;
        }
        if(result.length===0){
            res.send({code:500,msg:'用户列表拉取失败'});
          }else{
            res.send({code:200,msg:'用户列表拉取成功',data:result});
    }
     });
});

//5.更改企业发布信息接口
r.put('/info',(req,res,next)=>{
    var obj=req.body;
    console.log(req.body);
var i=400;
for(var k in obj){
    i++;
    if(obj[k]==''){
        res.send({code:i,msg:k+'不能为空'});
        return;
    }
}
    pool.query('update com_zhiwei set? where czw_id=?',[obj,obj.czw_id],(err,result)=>{
        console.log(result);
        if(err){
            next(err);
            return;
        }
        if(result.affectedRows===0){
            res.send({code:501,msg:'修改失败'});
          }else{
            res.send({code:200,msg:'修改成功',data:result});
          }
        });

});



//6.企业首页轮播图接口
r.post('/lunbo',(req,res,next)=>{
   
    var obj=req.body;
    console.log(obj);

    
    var i=400;
    for(var k in obj){
        i++;
        if(obj[k]==''){
            res.send({code:i,msg:k+'不能为空'});
            return;
        }
    }  
   
    //执行sql命令
    pool.query('insert into com_lunbo set?',[obj],(err,result)=>{
        if(err){
            next(err);
            return;
        }
        console.log(result);
        res.send({code:200,msg:'企业轮播图信息添加成功成功'});
    });

   
});














//暴露路由对象
module.exports=r;